#!/bin/sh
# 
#  This will run the NodeUnit tests. If `nodeunit` isn't installed, type:
#
#      sudo npm install nodeunit
#      sudo npm install jshint

if type jshint >/dev/null 2>&1
then
    FILES="`find examples lib -name '*.js'`"
    TMPHINT=/tmp/jshint-results
    
    for FILE in $FILES
    do
        echo "Analyzing: $FILE"
        HIDDEN_EXP="`dirname $FILE`/.`basename $FILE`-exp"
        jshint $FILE > $TMPHINT
        if [ $? -gt 0 ]
        then
            if [ -f "$HIDDEN_EXP" ]
            then
                diff $TMPHINT "$HIDDEN_EXP"
            else
                cat $TMPHINT
                echo
                echo "Note: If this is an expected (and accepted) warning, then:"
                echo "cp '$TMPHINT' '$HIDDEN_EXP'"
                echo
                exit 1
            fi
        fi
    done
fi

# Usage: nodeunit [options] testmodule1.js testfolder [...] 
# Options:
# 
#   --config FILE     the path to a JSON file with options
#   --reporter FILE   optional path to a reporter file to customize the output
#   --list-reporters  list available build-in reporters
#   -t name,          specify a test to run
#   -h, --help        display this help and exit
#   -v, --version     output version information and exit

# Build-in reporters: 
#   * browser: Browser-based test reporter
#   * default: Default tests reporter
#   * eclipse: Reporter for eclipse plugin
#   * html: Report tests result as HTML
#   * junit: jUnit XML test reports
#   * machineout: Tests reporter for machinally analysis
#   * minimal: Pretty minimal output
#   * nested: Nested test reporter
#   * skip_passed: Skip passed tests output
#   * tap: TAP output
#   * verbose: Verbose tests reporter

echo "Executing tests..."
# OPTS="--reporter junit --output test/results"

FILES="`find test -name 'test-*.js'` `find examples -name '*.js'`"

# echo nodeunit $OPTS $FILES
exec nodeunit $OPTS $FILES
